package com.yumingjiang.weichatgtp.common.thread;

import com.yumingjiang.commontool.util.LoggerUtil;
import com.yumingjiang.weichatgtp.common.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Administrator]
 * @version : [v1.0]
 * @createTime : [2023/3/10 22:45]
 */
public class TheadPoolExecutorUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(TheadPoolExecutorUtil.class);

    public static TheadPoolExecutorUtil getInstance() {
        return TheadPoolExecutorUtilHolder.instance;
    }

    private static class TheadPoolExecutorUtilHolder {

        private static TheadPoolExecutorUtil instance = new TheadPoolExecutorUtil();
    }

    private TheadPoolExecutorUtil() {
    }

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 60,
            TimeUnit.SECONDS, new LinkedBlockingDeque<>(20), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r);
        }
    }, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            LoggerUtil.error(LOGGER,new BizException(r.toString(), executor),"### 当前任务超限，被丢弃！！！");
        }
    });

    public ThreadPoolExecutor getThreadPoolExecutor() {
        return threadPoolExecutor;
    }
}
